package com.zmn.oms.zmn.business.impl.grid;

import com.alibaba.fastjson.JSON;
import com.google.api.client.util.Lists;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.categ.CategProductGroupDRO;
import com.zmn.base.product.common.query.categ.CategProductGroupQuery;
import com.zmn.base.product.dubbo.interfaces.categ.serv.CategServProductListRemoteService;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.biz.engineer.common.dro.distribute.EngineerDateTimeRangeDistributeOrderDRO;
import com.zmn.biz.engineer.common.query.distribute.DistributeOrderSkillQuery;
import com.zmn.biz.engineer.common.query.distribute.EngineerEnforceDistributeOrderQuery;
import com.zmn.biz.engineer.dubbo.interfaces.distribute.PlatDistributeOrderListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.engstock.common.model.dio.CheckEngineerStockDIO;
import com.zmn.engstock.common.model.dro.CheckMultiEngineerStockDRO;
import com.zmn.engstock.dubbo.interfaces.stock.StockListRemoteService;
import com.zmn.gms.common.dio.grid.online.gridgroup.EngineerInGroupDIO;
import com.zmn.gms.common.dto.online.gridgroup.GridGroupDRO;
import com.zmn.gms.dubbo.interfaces.grid.online.gridgroup.GridGroupListRemoteService;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightPlatBService;
import com.zmn.oms.business.interfaces.grid.GridGroupDistributeBService;
import com.zmn.oms.business.interfaces.orderstock.OrderStockBService;
import com.zmn.oms.common.dio.schedule.GridGroupScheduleCheckDIO;
import com.zmn.oms.common.dro.schedule.GridGroupScheduleCheckDRO;
import com.zmn.oms.model.bo.work.schedule.GridGroupScheduleBO;
import com.zmn.oms.model.dto.work.schedule.WorkOrderScheduleQuery;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightPlat;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 类描述: 网格组派单
 *
 * @author: HuangChao
 * @since: 2022/04/14 19:01
 */
@Slf4j
@Service
public class GridGroupDistributeBServiceImpl implements GridGroupDistributeBService {

    @DubboReference(version = com.zmn.gms.dubbo.interfaces.utils.DubboConsts.INTERFACE_VERSION, check = false)
    private GridGroupListRemoteService gridGroupListRemoteService;
    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private StockListRemoteService stockListRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private PlatDistributeOrderListRemoteService platDistributeOrderListRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private ProductForeignListRemoteService productForeignListRemoteService;
    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    protected CategServProductListRemoteService categServProductListRemoteService;

    @Autowired
    private OrderStockBService orderStockBService;
    @Autowired
    private DistributeWeightPlatBService distributeWeightPlatBService;
    @Autowired
    private ZsDistributeWorkBService zsDistributeWorkBService;

    @Override
    public Boolean checkGridGroupTodayAvailable(WorkOrderScheduleQuery query) {
        // 先根据经纬度查询网格可服务工程师
        log.info("checkGridGroupTodayAvailable:{}", JSON.toJSONString(query));
        GridGroupDRO gridGroupDRO = this.listGridEngineerIdByLatAndLng(query.getLatitude(), query.getLongitude(), query.getProductGroupId());
        if (CollectionUtil.isNotNullOrEmpty(gridGroupDRO.getEngineerIdList())) {
            // 是否开启动态库存 开启走网格组派单逻辑
            Date queryDate = Optional.ofNullable(DateUtil.parse(query.getScheDate(), DateUtil.FORMAT_DATE)).orElse(DateUtil.getNow());
            if (orderStockBService.isWhiteWorkCity(query.getCityId())) {
                // 过滤工程师
                List<Integer> filterEngineerIdList = this.filterEngineerDistribute(gridGroupDRO.getEngineerIdList(), query, queryDate);
                // 根据条件查询指定日期是否有可服务工程师
                if (CollectionUtil.isNotNullOrEmpty(filterEngineerIdList)) {
                    CheckEngineerStockDIO checkEngineerStockDIO = CheckEngineerStockDIO.builder()
                            .engineerIdList(filterEngineerIdList)
                            .productGroupCode(query.getProductGroupCode())
                            .productNum(query.getProductNum())
                            .queryDate(queryDate)
                            .build();
                    log.info("#StockListRemoteService#hasOneDayServiceEngineerByQuery入参：{}", JSON.toJSONString(checkEngineerStockDIO));
                    ResponseDTO<Boolean> responseDTO = stockListRemoteService.hasOneDayServiceEngineerByQuery(checkEngineerStockDIO);
                    log.info("#StockListRemoteService#hasOneDayServiceEngineerByQuery出参：{}", responseDTO);
                    if (responseDTO.isSuccess()) {
                        return Optional.ofNullable(responseDTO.getData()).orElse(Boolean.FALSE);
                    }
                }
            } else {
                // 未开启查指定日期是否存在可服务工程师
                return checkAvailableEngineer(gridGroupDRO.getEngineerIdList(), query, queryDate);
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 1、先根据经纬度查询网格可服务工程师
     * 2、循环查询七天是否存在可服务工程师
     * 2.1 过滤指定日期是否存在工程师可服务
     * 2.2 调用网格组接口查询指定日期是否可服务
     *
     * @param query
     * @return
     */
    @Override
    public GridGroupScheduleBO getGridGroupFirstAvailableDates(WorkOrderScheduleQuery query) {
        log.info("getGridGroupFirstAvailableDates:{}", JSON.toJSONString(query));
        // 先根据经纬度查询网格可服务工程师
        GridGroupDRO gridGroupDRO = this.listGridEngineerIdByLatAndLng(query.getLatitude(), query.getLongitude(), query.getProductGroupId());
        if (CollectionUtil.isNotNullOrEmpty(gridGroupDRO.getEngineerIdList())) {
            Date curDate = DateUtil.getDateStart(DateUtil.getNow());
            // 是否开启动态库存 开启走网格组派单逻辑
            if (orderStockBService.isWhiteWorkCity(query.getCityId())) {
                for (int i = 0; i < 7; i++) {
                    // 过滤工程师
                    Date queryDate = DateUtil.getDateStart(DateUtil.getNowDefer(i));
                    List<Integer> filterEngineerIdList = this.filterEngineerDistribute(gridGroupDRO.getEngineerIdList(), query, queryDate);
                    // 根据条件查询指定日期是否有可服务工程师
                    if (CollectionUtil.isNotNullOrEmpty(filterEngineerIdList)) {
                        CheckEngineerStockDIO checkEngineerStockDIO = CheckEngineerStockDIO.builder()
                                .engineerIdList(filterEngineerIdList)
                                .productGroupCode(query.getProductGroupCode())
                                .productNum(query.getProductNum())
                                .queryDate(queryDate)
                                .build();
                        log.info("#StockListRemoteService#hasOneDayServiceEngineerByQuery入参：{}", JSON.toJSONString(checkEngineerStockDIO));
                        ResponseDTO<Boolean> responseDTO = stockListRemoteService.hasOneDayServiceEngineerByQuery(checkEngineerStockDIO);
                        log.info("#StockListRemoteService#hasOneDayServiceEngineerByQuery出参：{}", responseDTO);
                        if (responseDTO.isSuccess() && responseDTO.getData()) {
                            GridGroupScheduleBO gridGroupScheduleBO = new GridGroupScheduleBO();
                            if (Objects.equals(curDate, queryDate)) {
                                gridGroupScheduleBO.setHasEngineerFlag(Boolean.TRUE);
                            } else {
                                gridGroupScheduleBO.setHasEngineerFlag(Boolean.FALSE);
                            }
                            gridGroupScheduleBO.setCanServiceDate(queryDate);
                            gridGroupScheduleBO.setGridGroupType(gridGroupDRO.getEngineerGridType());
                            return gridGroupScheduleBO;
                        }
                    }
                }
            } else {
                // 未开启动态库存 查当前到七天是否有工程师可服务
                for (int i = 0; i < 7; i++) {
                    Date queryDate = DateUtil.getDateStart(DateUtil.getNowDefer(i));
                    if (checkAvailableEngineer(gridGroupDRO.getEngineerIdList(), query, queryDate)) {
                        GridGroupScheduleBO gridGroupScheduleBO = new GridGroupScheduleBO();
                        gridGroupScheduleBO.setCanServiceDate(queryDate);
                        if (Objects.equals(curDate, queryDate)) {
                            gridGroupScheduleBO.setHasEngineerFlag(Boolean.TRUE);
                        } else {
                            gridGroupScheduleBO.setHasEngineerFlag(Boolean.FALSE);
                        }
                        gridGroupScheduleBO.setCanServiceDate(queryDate);
                        gridGroupScheduleBO.setGridGroupType(gridGroupDRO.getEngineerGridType());
                        return gridGroupScheduleBO;
                    }
                }
            }
        }
        // 无可服务工程师 默认返回七天后可服务
        GridGroupScheduleBO gridGroupScheduleBO = new GridGroupScheduleBO();
        gridGroupScheduleBO.setHasEngineerFlag(Boolean.FALSE);
        gridGroupScheduleBO.setGridGroupType(gridGroupDRO.getEngineerGridType());
        return gridGroupScheduleBO;
    }

    /**
     * 过滤不符合的工程师
     *
     * @param engineerIdList 网格工程师id集合
     * @param query          排期查询条件
     * @param queryDate      查询日期
     * @return 符合条件的工程id集合
     */
    private List<Integer> filterEngineerDistribute(List<Integer> engineerIdList, WorkOrderScheduleQuery query, Date queryDate) {
        log.info("#GridGroupDistributeBService#filterEngineerDistribute调用");
        if (CollectionUtil.isNotNullOrEmpty(engineerIdList)) {
            EngineerEnforceDistributeOrderQuery platDirectlySendQuery = new EngineerEnforceDistributeOrderQuery();

            platDirectlySendQuery.setSerialNumber(UUID.randomUUID().toString());

            platDirectlySendQuery.setPlat(query.getPlatWork());
            platDirectlySendQuery.setBizType(query.getBizType());
            platDirectlySendQuery.setCityId(query.getCityId());
            platDirectlySendQuery.setOrderLat(query.getLatitude());
            platDirectlySendQuery.setOrderLng(query.getLongitude());
            platDirectlySendQuery.setDutyTime(queryDate);
            platDirectlySendQuery.setServCategId(query.getServCategId());
            platDirectlySendQuery.setProductGroupId(query.getProductGroupId());
            platDirectlySendQuery.setMatchEngineerList(engineerIdList);
            List<List<DistributeOrderSkillQuery>> skillLists = zsDistributeWorkBService.buildSkillQueryLists(query.getProductId(), query.getShowProductId(), query.getServCategId(), query.getCategId());
            platDirectlySendQuery.setServSkillList(skillLists);
            log.info("filterEngineerDistribute#listForDutyTimeDistributeOrder：入参  {}", JSON.toJSONString(platDirectlySendQuery));
            ResponseDTO<List<Integer>> listResponseDTO = platDistributeOrderListRemoteService.listForDutyTimeDistributeOrder(platDirectlySendQuery);
            log.info("filterEngineerDistribute#listForDutyTimeDistributeOrder：出参 {}", JSON.toJSONString(listResponseDTO));
            if (listResponseDTO.isSuccess() && CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
                return listResponseDTO.getData();
            }
        }
        return Lists.newArrayList();
    }

    /**
     * 校验指定日期是否有满足条件的工程师
     *
     * @param engineerIdList 网格工程师id集合
     * @param query          排期查询条件
     * @param queryDate      查询日期
     * @return
     */
    private Boolean checkAvailableEngineer(List<Integer> engineerIdList, WorkOrderScheduleQuery query, Date queryDate) {
        log.info("#GridGroupDistributeBService#checkAvailableEngineer调用");
        if (CollectionUtil.isNotNullOrEmpty(engineerIdList)) {
            EngineerEnforceDistributeOrderQuery platDirectlySendQuery = new EngineerEnforceDistributeOrderQuery();

            platDirectlySendQuery.setSerialNumber(UUID.randomUUID().toString());

            platDirectlySendQuery.setPlat(query.getPlat());
            platDirectlySendQuery.setBizType(query.getBizType());
            platDirectlySendQuery.setCityId(query.getCityId());
            platDirectlySendQuery.setOrderLat(query.getLatitude());
            platDirectlySendQuery.setOrderLng(query.getLongitude());
            platDirectlySendQuery.setDutyTime(queryDate);
            platDirectlySendQuery.setServCategId(query.getServCategId());
            platDirectlySendQuery.setProductGroupId(query.getProductGroupId());
            platDirectlySendQuery.setMatchEngineerList(engineerIdList);
            List<List<DistributeOrderSkillQuery>> skillLists = zsDistributeWorkBService.buildSkillQueryLists(query.getProductId(), query.getShowProductId(), query.getServCategId(), query.getCategId());
            platDirectlySendQuery.setServSkillList(skillLists);
            log.info("checkAvailableEngineer#listForDutyTimeDistributeOrder 入参 [{}]",JSON.toJSONString(platDirectlySendQuery));
            ResponseDTO<List<Integer>> listResponseDTO = platDistributeOrderListRemoteService.listForDutyTimeDistributeOrder(platDirectlySendQuery);
            log.info("checkAvailableEngineer#listForDutyTimeDistributeOrder 出参 [{}]",JSON.toJSONString(listResponseDTO));
            if (listResponseDTO.isSuccess() && CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 通过经纬度获取网格工程师id集合
     *
     * @param lng            经度
     * @param lat            纬度
     * @param productGroupId 产品组id
     * @return 工程师id集合
     */
    private GridGroupDRO listGridEngineerIdByLatAndLng(Double lat, Double lng, Integer productGroupId) {
        EngineerInGroupDIO engineerInGroupDIO = EngineerInGroupDIO.builder()
                .lat(lat)
                .lng(lng)
                .serviceId(String.valueOf(productGroupId))
                .build();
        log.info("#GridGroupListRemoteService#listAvaliableEngineerByQuery入参：{}", JSON.toJSONString(engineerInGroupDIO));
        ResponseDTO<GridGroupDRO> responseDTO = gridGroupListRemoteService.listAvaliableEngineerAndGridTypeByQuery(engineerInGroupDIO);
        log.info("#GridGroupListRemoteService#listAvaliableEngineerByQuery出参：{}", JSON.toJSONString(responseDTO));
        if (responseDTO.isSuccess() && Objects.nonNull(responseDTO.getData())) {
            return responseDTO.getData();
        }
        // 获取失败默认返回无网格无工程师
        GridGroupDRO gridGroupDRO = GridGroupDRO.builder().engineerGridType(1).build();
        return gridGroupDRO;
    }

    /**
     * 批量查询网格组是否有可服务工程师
     * @param gridGroupScheduleCheckList
     * @return
     */
    @Override
    public List<GridGroupScheduleCheckDRO> listGridGroupAvailableEngineer(List<GridGroupScheduleCheckDIO> gridGroupScheduleCheckList) {
        if (CollectionUtil.isNullOrEmpty(gridGroupScheduleCheckList)) {
            return Collections.emptyList();
        }

        List<GridGroupScheduleCheckDRO> list = Lists.newArrayList();
        for (GridGroupScheduleCheckDIO gridGroupScheduleCheckDIO : gridGroupScheduleCheckList) {
            // 1、是否平台直派
            // 获取平台直派配置
            DistributeWeightPlat distributeWeightPlat =
                    distributeWeightPlatBService.findPlatDirectlyConfigByPlatAndBizTypeAndCityId(gridGroupScheduleCheckDIO.getPlat()
                            , gridGroupScheduleCheckDIO.getBizType()
                            , gridGroupScheduleCheckDIO.getCityId());

            // 计算是否应该平台直派
            if (Objects.isNull(distributeWeightPlat)) {
                return Collections.emptyList();
            }
            // 2、查询网格组工程师
            // 2.1 查询产品组信息
            CategProductGroupDRO productGroupDRO = this.getProductGroupByCateInfo(gridGroupScheduleCheckDIO.getBizType(), gridGroupScheduleCheckDIO.getServCategId(), gridGroupScheduleCheckDIO.getCategId(), BaseProductConsts.EC_SHOW_TYPE);
            if (Objects.isNull(productGroupDRO)) {
                log.info("获取产品组信息失败");
                List<GridGroupScheduleCheckDRO> defaultUnAvailable = this.getDefaultUnAvailable(gridGroupScheduleCheckDIO);
                list.addAll(defaultUnAvailable);
                continue;
            }
            gridGroupScheduleCheckDIO.setProductGroupId(productGroupDRO.getGroupId());
            gridGroupScheduleCheckDIO.setProductGroupCode(productGroupDRO.getGroupCode());
            GridGroupDRO gridGroupDRO = this.listGridEngineerIdByLatAndLng(gridGroupScheduleCheckDIO.getLatitude(), gridGroupScheduleCheckDIO.getLongitude(), gridGroupScheduleCheckDIO.getProductGroupId());
            if (CollectionUtil.isNullOrEmpty(gridGroupDRO.getEngineerIdList())) {
                List<GridGroupScheduleCheckDRO> defaultUnAvailable = this.getDefaultUnAvailable(gridGroupScheduleCheckDIO);
                list.addAll(defaultUnAvailable);
                continue;
            }
            // 3、过滤工程师
            List<EngineerDateTimeRangeDistributeOrderDRO> engineerDROS = this.filterEngineerDistributeDates(gridGroupDRO.getEngineerIdList(), gridGroupScheduleCheckDIO);
            if (CollectionUtil.isNullOrEmpty(engineerDROS)) {
                List<GridGroupScheduleCheckDRO> defaultUnAvailable = this.getDefaultUnAvailable(gridGroupScheduleCheckDIO);
                list.addAll(defaultUnAvailable);
                continue;
            }
            // 4、是否开启库存
            if (orderStockBService.isWhiteWorkCity(gridGroupScheduleCheckDIO.getCityId())) {
                List<CheckMultiEngineerStockDRO> stockDROS = this.listDayServiceToStock(engineerDROS, gridGroupScheduleCheckDIO);
                if (CollectionUtil.isNullOrEmpty(stockDROS)) {
                    List<GridGroupScheduleCheckDRO> defaultUnAvailable = this.getDefaultUnAvailable(gridGroupScheduleCheckDIO);
                    list.addAll(defaultUnAvailable);
                    continue;
                }

                List<GridGroupScheduleCheckDRO> checkDROS = stockDROS.stream().map(dro -> {
                    GridGroupScheduleCheckDRO gridGroupScheduleCheckDRO = new GridGroupScheduleCheckDRO();
                    gridGroupScheduleCheckDRO.setShowProductId(gridGroupScheduleCheckDIO.getShowProductId());
                    gridGroupScheduleCheckDRO.setQueryDate(dro.getQueryDate());
                    gridGroupScheduleCheckDRO.setHasEngineer(dro.getHasEngineer());
                    return gridGroupScheduleCheckDRO;
                }).collect(Collectors.toList());
                list.addAll(checkDROS);
            } else {
                // 未开启 过滤后存在工程师 即可服务
                List<GridGroupScheduleCheckDRO> checkDROS = engineerDROS.stream().map(e -> {
                    GridGroupScheduleCheckDRO gridGroupScheduleCheckDRO = new GridGroupScheduleCheckDRO();
                    gridGroupScheduleCheckDRO.setShowProductId(gridGroupScheduleCheckDIO.getShowProductId());
                    gridGroupScheduleCheckDRO.setQueryDate(e.getDutyTime());
                    gridGroupScheduleCheckDRO.setHasEngineer(Boolean.FALSE);
                    if (CollectionUtil.isNotNullOrEmpty(e.getEngineerIdList())) {
                        gridGroupScheduleCheckDRO.setHasEngineer(Boolean.TRUE);
                    }
                    return gridGroupScheduleCheckDRO;
                }).collect(Collectors.toList());
                list.addAll(checkDROS);
            }
        }
        return list;
    }

    /**
     * 获取默认不可服务返回结果集合
     * @param gridGroupScheduleCheckDIO
     */
    private List<GridGroupScheduleCheckDRO> getDefaultUnAvailable(GridGroupScheduleCheckDIO gridGroupScheduleCheckDIO) {
        List<GridGroupScheduleCheckDRO> list = Lists.newArrayListWithCapacity(gridGroupScheduleCheckDIO.getDays());
        for (Integer i = 0; i < gridGroupScheduleCheckDIO.getDays(); i++) { 
            GridGroupScheduleCheckDRO dro = new GridGroupScheduleCheckDRO();
            dro.setShowProductId(gridGroupScheduleCheckDIO.getShowProductId());
            dro.setQueryDate(DateUtil.addDays(gridGroupScheduleCheckDIO.getScheDate(), i));
            dro.setHasEngineer(Boolean.FALSE);
            list.add(dro);
        }
        return list;
    }


    /**
     * 获产品组信息
     * @param bizType
     * @param servCategId
     * @param categId
     * @param showType
     * @return
     */
    private CategProductGroupDRO getProductGroupByCateInfo(Integer bizType, Integer servCategId, Integer categId, Integer showType) {
        CategProductGroupQuery categProductGroupQuery = new CategProductGroupQuery();
        categProductGroupQuery.setBizType(bizType);
        categProductGroupQuery.setServCategId(servCategId);
        categProductGroupQuery.setCategId(categId);
        categProductGroupQuery.setShowType(showType);
        log.info("servAssCategoryListRemoteService#getProductGroupByShowCategId 查询产品分组 入参：{}", categProductGroupQuery);
        ResponseDTO<CategProductGroupDRO> responseDTO = categServProductListRemoteService.getBasisProductGroupByQuery(categProductGroupQuery);
        log.info("servAssCategoryListRemoteService#getProductGroupByShowCategId 查询产品分组 出参：【{}】", responseDTO);
        if (responseDTO.isSuccess() && Objects.nonNull(responseDTO.getData())) {
            return   responseDTO.getData();
        }
        return null;
    }

    /**
     * 多日期-过滤工程师
     * @param engineerIdList
     * @param gridGroupScheduleCheckDIO
     */
    private List<EngineerDateTimeRangeDistributeOrderDRO> filterEngineerDistributeDates(List<Integer> engineerIdList, GridGroupScheduleCheckDIO gridGroupScheduleCheckDIO) {
        if (CollectionUtil.isNotNullOrEmpty(engineerIdList)) {
            EngineerEnforceDistributeOrderQuery platDirectlySendQuery = new EngineerEnforceDistributeOrderQuery();
            platDirectlySendQuery.setSerialNumber(UUID.randomUUID().toString());
            platDirectlySendQuery.setPlat(gridGroupScheduleCheckDIO.getPlat());
            platDirectlySendQuery.setBizType(gridGroupScheduleCheckDIO.getBizType());
            platDirectlySendQuery.setCityId(gridGroupScheduleCheckDIO.getCityId());
            platDirectlySendQuery.setOrderLat(gridGroupScheduleCheckDIO.getLatitude());
            platDirectlySendQuery.setOrderLng(gridGroupScheduleCheckDIO.getLongitude());
            platDirectlySendQuery.setDutyTime(gridGroupScheduleCheckDIO.getScheDate());
            platDirectlySendQuery.setServCategId(gridGroupScheduleCheckDIO.getServCategId());
            platDirectlySendQuery.setProductGroupId(gridGroupScheduleCheckDIO.getProductGroupId());
            platDirectlySendQuery.setMatchEngineerList(engineerIdList);
            List<List<DistributeOrderSkillQuery>> skillLists = zsDistributeWorkBService.buildSkillQueryLists(null, gridGroupScheduleCheckDIO.getShowProductId(), gridGroupScheduleCheckDIO.getServCategId(), gridGroupScheduleCheckDIO.getCategId());
            platDirectlySendQuery.setServSkillList(skillLists);
            platDirectlySendQuery.setQueryDays(gridGroupScheduleCheckDIO.getDays() - 1);
            log.info("filterEngineerDistributeDates#listForDutyTimeRangeDistributeOrder 入参 [{}]",JSON.toJSONString(platDirectlySendQuery));
            ResponseDTO<List<EngineerDateTimeRangeDistributeOrderDRO>> listResponseDTO = platDistributeOrderListRemoteService.listForDutyTimeRangeDistributeOrder(platDirectlySendQuery);
            log.info("filterEngineerDistributeDates#listForDutyTimeRangeDistributeOrder 出参 [{}]",JSON.toJSONString(listResponseDTO));
            if (listResponseDTO.isSuccess() && CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
                return listResponseDTO.getData();
            }
        }
        return Collections.emptyList();
    }

    /**
     * 批量查询日期库存系统是否有可服务工程师
     * @param list
     * @param gridGroupScheduleCheckDIO
     * @return
     */
    private List<CheckMultiEngineerStockDRO> listDayServiceToStock(List<EngineerDateTimeRangeDistributeOrderDRO> list, GridGroupScheduleCheckDIO gridGroupScheduleCheckDIO) {
        if (CollectionUtil.isNullOrEmpty(list)) {
            log.info("工程师数据集合为空，返回空集合");
            return Collections.emptyList();
        }

        List<CheckEngineerStockDIO> checkEngineerStockDIOList = list.stream().map(o -> {
            CheckEngineerStockDIO dio = new CheckEngineerStockDIO();
            dio.setProductGroupCode(gridGroupScheduleCheckDIO.getProductGroupCode());
            dio.setProductNum(gridGroupScheduleCheckDIO.getProductNum());
            dio.setEngineerIdList(o.getEngineerIdList());
            dio.setQueryDate(o.getDutyTime());
            return dio;
        }).collect(Collectors.toList());
        log.info("StockListRemoteService#listDayServiceEngineerByQuery 入参：[{}]", checkEngineerStockDIOList);
        ResponseDTO<List<CheckMultiEngineerStockDRO>> listResponseDTO = stockListRemoteService.listDayServiceEngineerByQuery(checkEngineerStockDIOList);
        log.info("StockListRemoteService#listDayServiceEngineerByQuery 出参：[{}]", listResponseDTO);
        if (listResponseDTO.isSuccess() && CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
            return listResponseDTO.getData();
        }
        return Collections.emptyList();
    }
}